$("#table").bootstrapTable({
    url: '/super/privilegeManage.html',
    contentType: 'application/x-www-form-urlencoded',
    method: 'POST',
    toolbar: '#toolbar',
    sidePagination: 'server',
    pageNumber: 1,//当前页
    pageSize: 15,//每页条数
    pageList: [15, 50, 100, 'ALL'],
    dataType: 'json',//返回数据的类型
    /*detailView: true,*/
    cache: false,
    pagination: true,//使用分页
    /*showRefresh: true,//右上角刷新按钮
    showColumns: true,//右上角,列,列表*/
    queryParams:function (params) {
        params["parentId"] = $("#parent-select").val();
        return params;
    },
    classes: 'table table-hover table-striped',
    columns: [{
        checkbox: true
    }, {
        field: 'number',
        title: '序号',
        align: 'center',
        formatter: function (value, row, index) {
            return (index + 1);
        },
    }, {
        field: "id",
        title: "编号",
        align: "center"
    }, {
        field: "name",
        title: "名称",
        align: "center"
    }, {
        field: "href",
        title: "跳转路径",
        align: "center",
        formatter: function (value, row, index) {
            if (row.href.length > 10) {
                return "<a class='table-in' href='javascript:void(0);' title='" + row.href + "'>" + row.href.substring(0, 10) + "..." + "</a>";
            } else {
                return row.href;
            }
        }
    }, {
        field: "parent",
        title: "父权限",
        align: "center",
        formatter: function (value, row, index) {
            if (row.parent != null) {
                return row.parent.id;
            } else {
                return "-";
            }
        }
    }, {
        field: "icon",
        title: "图标",
        align: "center",
        formatter: function (value, row, index) {
            if (row.icon.length > 10) {
                return "<a class='table-in' href='javascript:void(0)' title='" + row.icon + "'>" + row.icon.substring(0, 10) + "</a>";
            } else {
                return row.icon;
            }
        }
    }, {
        field: "operator",
        title: "操作",
        align: "center",
        formatter: function (value, row, index) {
            var str = "<span class='glyphicon glyphicon-pencil' onclick='changePrivilege(" + row.id + ")'></span>&nbsp&nbsp;" +
                "<span class='glyphicon glyphicon-trash' onclick='delPrivilegeById(" + row.id + ")'></span>&nbsp&nbsp;";

            return str;
        }
    }]
});

/**
 * 打开修改权限的模态框并显示默认数据
 * @param id 默认显示的权限编号
 */
function changePrivilege(id) {
    var url = "/super/getOnePrivilegeById";
    $("#changePrivilegeModal").modal('show');
    $("#changePrivilegeModal .modal-body").load(url, {id: id});
}

/**
 * 修改权限表单提交
 */
function changePrivilegeSubmit() {
    $.post("/super/changePrivilege",
        {
            "id": $("#id").val(),
            "name": $("#name").val(),
            "href": $("#href").val(),
            "icon": $("#icon").val(),
            "parent": $("#parent").val()
        }, function (data) {
            if (data.updates) {
                $("#table").bootstrapTable('refresh');
                swal('修改成功!', '权限修改成功,请重新登录!', 'success');
                $("#changePrivilegeModal").modal('hide');
            } else {
                swal('修改失败!', '权限修改失败,请重试!', 'error');
                $("#changePrivilegeModal").modal('hide');
            }
        }, "json");
}

/**
 * 添加权限
 */
function addPrivilege() {
    $("#addPrivilegeModal").modal("show");
    $("#addPrivilegeModal .modal-body").load("/super/openAddPrivilege");
}

/**
 * 添加权限提交
 */
function addPrivilegeSubmit() {
    $.post("/super/addPrivilege",
        {
            "name": $("#name").val(),
            "href": $("#href").val(),
            "icon": $("#icon").val(),
            "parent": $("#parent").val()
        }, function (data) {
            if (data.updates) {
                $("#table").bootstrapTable('refresh');
                swal('添加成功!', '权限添加成功,请重新登录!', 'success');
                $("#addPrivilegeModal").modal('hide');
            } else {
                swal('添加失败!', '权限添加失败,请重试!', 'error');
                $("#addPrivilegeModal").modal('hide');
            }
        }, "json"
    );
}

/**
 * 根据编号删除权限
 * @param id 将要删除的权限编号
 */
function delPrivilegeById(id) {
    swal({
            title: "确认删除?",
            text: "如果这是父权限，那么会将对应的子权限也一并删除!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                $.post("/super/delPrivilegeById", {"id": id}, function (data) {
                    if (data.updates >= 1) {
                        $("#table").bootstrapTable("refresh");
                        swal("删除成功!", "权限删除成功!共删除了" + data.updates + "条权限!请重新登录!", "success");
                    } else {
                        swal("删除失败!", "权限删除失败，请重试!");
                    }
                }, "json");
            }else{
                swal("删除失败!", "您取消了删除操作!)", "error");
            }
        });
}

/**
 * 根据父权限编号获取权限
 */
function getPrivilegeByParentId() {
    $("#table").bootstrapTable("refresh");
}